package cn.changeforyou.analyzer.combine.orm.compare.compareResultProcessor;

import cn.changeforyou.analyzer.combine.orm.compare.EntityAndXmlComparator.XmlCompareResultNode;
import cn.changeforyou.analyzer.combine.orm.compare.compareResultProcessor.cmdParamters.DeleteParameters;
import cn.changeforyou.analyzer.combine.orm.compare.compareResultProcessor.cmdParamters.InsertAndDeleteParameters;
import cn.changeforyou.analyzer.combine.orm.compare.compareResultProcessor.cmdParamters.InsertParameters;
import cn.changeforyou.analyzer.xmlparse.mybatis.reader.BaseColumnListReaderWriter;
import cn.changeforyou.cmd.generator.CmdGenerator;
import cn.changeforyou.cmd.generator.CmdGeneratorParameter;
import cn.changeforyou.cmd.generator.GeneratorContext;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.dom4j.io.XMLWriter;

/**
 * @author zhyu
 * @date 2021/2/19 11:06
 */
public class InsertAndDeleteXmlLabelCompareResultProcessor implements CmdGenerator {
	
	Class<? extends CmdGeneratorParameter> cmdGeneratorParameter;
	boolean auto;
	private XMLWriter writer;
	private XmlCompareResultNode resultNode;
	private List<String> labelMoreField;
    private List<String> entityMoreField;
    
    @Override
	public String getSign() {
		return "insertAndDeleteXmlLabelCompareResultProcessor";
	}
	
	@Override
	public boolean generate_do(GeneratorContext context, CmdGeneratorParameter params) {
		if (params instanceof InsertParameters) {
			InsertParameters insertParameters = (InsertParameters) params;
			auto = insertParameters.isAll();
			if (auto) {
			} else {
				String insertNames = (insertParameters).getInsertNames();
				String[] names = getStrings(insertNames);
				new BaseColumnListReaderWriter().write(names, resultNode, true);
			}
		} else if (params instanceof DeleteParameters) {
			DeleteParameters deleteParameters = (DeleteParameters) params;
			auto = deleteParameters.isAll();
			if (auto) {
			} else {
				String deleteFieldNames = (deleteParameters).getDeleteFieldNames();
				String[] names = getStrings(deleteFieldNames);
			}
		} else {
			InsertAndDeleteParameters insertAndDelete = (InsertAndDeleteParameters) params;
			auto = insertAndDelete.isAll();
			if (auto) {
			} else {
				String insertNames = insertAndDelete.getInsertNames();
				String deleteFieldNames = insertAndDelete.getDeleteFieldNames();
			}
		}
		
		return true;
	}
	
	
	private String[] getStrings(String insertNames) {
		String[] split = insertNames.split(",");
		for (int i = 0; i < split.length; i++) {
			split[i] = split[i].trim();
		}
		return split;
	}
	
	@Override
	public String getName() {
		return "修改" + resultNode.getXmlCodeType().getDescription() + "处理器";
	}
	
	@Override
	public Class getParameterClass() {
		return cmdGeneratorParameter;
	}
	
	@Override
	public void init(GeneratorContext context) {
		resultNode = context.getFromClassMap(XmlCompareResultNode.class);
		try {
			writer = new XMLWriter(new FileOutputStream(context.getFromClassMap(File.class)));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		labelMoreField = resultNode.getXmlLabelMoreField();
		entityMoreField = resultNode.getEntityMoreField();
		int operateCount = 0;
		
		if (CollectionUtils.isNotEmpty(entityMoreField)) {
			operateCount++;
			StringBuilder str = new StringBuilder("实体类多出的字段为: \n");
			for (String field : entityMoreField) {
				str.append(field).append(",");
			}
			str.deleteCharAt(str.length() - 1);
			log.info(str.toString());
			
		}
		
		if (CollectionUtils.isNotEmpty(labelMoreField)) {
			operateCount += 2;
			StringBuilder str = new StringBuilder("xml标签多出的字段为: \n");
			for (String field : labelMoreField) {
			    str.append(field).append(",");
			}
			str.deleteCharAt(str.length() - 1);
			log.info(str.toString());
		}
		switch (operateCount) {
			case 1:
				cmdGeneratorParameter = InsertParameters.class;
				break;
			case 2:
				cmdGeneratorParameter = DeleteParameters.class;
				break;
			default:
				cmdGeneratorParameter = InsertAndDeleteParameters.class;
		}
		log.info("请使用命令操作要删除或者添加的字段");
	}
}
